local skel = fk.CreateSkill {
  name = "emo__wanqi",
}

Fk:loadTranslationTable{
  ["emo__wanqi"] = "万起",
  [":emo__wanqi"] = "出牌阶段限三次，你可以将一张红色牌当【过河拆桥】/【决斗】对手牌/体力最多的角色的角色使用，若其因此脱离最多，手牌/体力最少的角色均可以令你摸一张牌。",

  ["#emo__wanqi"] = "万起：将1张红色牌当【过河拆桥】/【决斗】对手牌/体力最多的角色的角色使用",
  ["#emo__wanqi-draw"] = "万起：你可以令 %src 摸1张牌",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "dismantlement,duel",
  prompt = "#emo__wanqi",
  interaction = function(self, player)
    return UI.CardNameBox { choices = {"dismantlement", "duel"} }
  end,
  handly_pile = true,
  filter_pattern = {
    min_num = 1, max_num = 1, pattern = ".|.|red",
  },
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local name = self.interaction.data
    if Fk.all_card_types[name] == nil then return end
    local c = Fk:cloneCard(name)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function (self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data.orgi_tos = use.tos
  end,
  after_use = function (self, player, use)
    if player.dead then return end
    local room = player.room
    if use.extra_data and use.extra_data.orgi_tos then
      local checkHp = use.card.trueName == "duel"
      local max = 0
      for _, p in ipairs(room.alive_players) do
        max = math.max(max, checkHp and p.hp or p:getHandcardNum())
      end
      local times = 0
      for _, to in ipairs(use.extra_data.orgi_tos) do
        if (checkHp and to.hp or to:getHandcardNum()) < max then
          times = times + 1
        end
      end
      for _ = 1, times do
        local min = checkHp and player.hp or player:getHandcardNum()
        for _, p in ipairs(room.alive_players) do
          min = math.min(min, checkHp and p.hp or p:getHandcardNum())
        end
        local players = table.filter(room:getAlivePlayers(), function (p)
          return (checkHp and p.hp or p:getHandcardNum()) == min
        end)
        room:doIndicate(player, players)
        for _, p in ipairs(players) do
          if not p.dead then
            if player.dead then return end
            if room:askToSkillInvoke(p, {skill_name = skel.name, prompt = "#emo__wanqi-draw:"..player.id }) then
              player:drawCards(1, skel.name)
            end
          end
        end
      end
    end
  end,
  times = function (self, player)
    return 3 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < 3
  end,
  enabled_at_response = function (self, player, response)
    return not response and player:usedSkillTimes(skel.name, Player.HistoryPhase) < 3
  end,
})

skel:addEffect("prohibit", {
  is_prohibited = function (self, from, to, card)
    if table.contains(card.skillNames, skel.name) then
      local max = 0
      if card.trueName == "duel" then
        for _, p in ipairs(Fk:currentRoom().alive_players) do
          max = math.max(max, p.hp)
        end
        return to.hp < max
      else
        for _, p in ipairs(Fk:currentRoom().alive_players) do
          max = math.max(max, p:getHandcardNum())
        end
        return to:getHandcardNum() < max
      end
    end
  end,
})

return skel
